package JIP::Utils;

use JIP::ToolSet; # load my commonly-used modules in a single import
use Exporter::Lite;
use Encode qw( _utf8_on _utf8_off is_utf8 from_to );
use Time::localtime qw( localtime );

our @EXPORT_OK = qw(
    getLogName
    strictifyUTF
);

sub getLogName {
    my $tm = localtime;

    return sprintf(
        "%04d%02d%02d_%02d%02d%02d.log",
        $tm->year + 1900,
        $tm->mon + 1,
        $tm->mday,
        $tm->hour,
        $tm->min,
        $tm->sec
    );
}

sub strictifyUTF {
    my $text = shift;

    if ( is_utf8( $text ) && ! is_utf8( $text, 1 ) ) {
        _utf8_off( $text );
        from_to( $text, 'utf8', 'UTF-8' );
        _utf8_on( $text );
    }

    return $text;
}

1;
